<?php 
//I.F. Nuevo: 2010-09-15; ramarin

require_once('../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/report/moosisca/workflowlib.php');

require_once($CFG->libdir . '/csvlib.class.php');
require_once('export_form.php');

$sort         = optional_param('sort', '', PARAM_ALPHA);
$dir          = optional_param('dir', 'ASC', PARAM_ALPHA);
$extrasql     = optional_param('extrasql', '', PARAM_RAW);

if(isset($extrasql) && $extrasql != ''){
  $extrasql = unserialize(base64_decode($extrasql));
}
else{
  $extrasql = '';
}

$systemcontext = get_context_instance(CONTEXT_SYSTEM);
$viewUsers = has_capability('report/moosisca:viewallworkflow', $systemcontext) ? true : false;

// create the workflow filter form
$courses = get_workflows_listing(true, $viewUsers, $sort, $dir, null, null, '', '', '', $extrasql);

$fields = array();
$fields["type"] = get_string("spaceType", "report_moosisca");
$fields["course"] = get_string("course");
$fields["actualState"] = get_string("actualState", "report_moosisca");

if($viewUsers){
    $fields["userRequest"] = get_string("userRequest", "report_moosisca");
}

$fields["date"] = get_string("date");
$fields["state"] = get_string("state", "report_moosisca");
$fields["user"] = get_string("user");

$mform = new mod_data_export_form('workflow.php', $fields);

if ($formdata = (array) $mform->get_data()) {
    $exportdata = array();

    // populate the header in first row of export
    foreach($fields as $key => $field) {  
        $exportdata[0][] = $fields[$key];
    }

    $line = 1;
    $spaceTypes = moosisca_get_spaces_types();  
    
    foreach($courses as $course){    
        $state = get_string('workFlowState.' . $course->state, 'report_moosisca');
        $type = $spaceTypes[$course->space_type];
        
        $workflowbutton = "";

        if($viewUsers){
            $user = $course->realname;
        }

        $workflows = get_history_workflow("mwh.state, concat(u.firstname, ' ', u.lastname) realname, mwh.timecreated", "mwh.workflowid = " . $course->id);

        foreach($workflows as $workflow){            
            $data = array ();
            $data[] = $type;
            $data[] = format_string($course->fullname);
            $data[] = $state;

            if($viewUsers){
                $data[] = $user;
            }

            $data[] = userdate($workflow["timecreated"], '%Y/%m/%d');
            $data[] = get_string('workFlowState.' . $workflow["state"], 'report_moosisca');
            $data[] = $workflow["realname"];
            
            $exportdata[$line] = $data;
            $line++;
        }        
    }
    
    $line--;

    switch ($formdata['exporttype']) {
      case 'xls':
          data_export_xls($exportdata, 'workflow', $line);
          break;
      case 'ods':
          data_export_ods($exportdata, 'workflow', $line);
          break;
    }  
}

function data_export_xls($export, $dataname, $count) {
    global $CFG;
    require_once("$CFG->libdir/excellib.class.php");
    $filename = clean_filename("${dataname}-${count}_record");
    if ($count > 1) {
        $filename .= 's';
    }
    $filename .= clean_filename('-' . gmdate("Ymd_Hi"));
    $filename .= '.xls';
    
    $workbook = new MoodleExcelWorkbook('-');
    $workbook->send($filename);
    $worksheet = array();
    $worksheet[0] =& $workbook->add_worksheet('');
    $rowno = 0;
    
    foreach ($export as $row) {    
      $colno = 0;
      foreach($row as $col) {
        $worksheet[0]->write($rowno, $colno, $col);
        $colno++;
      }
      $rowno++;
    }
    $workbook->close();
    die;
}


function data_export_ods($export, $dataname, $count) {
    global $CFG;
    require_once("$CFG->libdir/odslib.class.php");
    $filename = clean_filename("${dataname}-${count}_record");
    if ($count > 1) {
        $filename .= 's';
    }
    $filename .= clean_filename('-' . gmdate("Ymd_Hi"));
    $filename .= '.ods';
    $workbook = new MoodleODSWorkbook('-');
    $workbook->send($filename);
    $worksheet = array();
    $worksheet[0] =& $workbook->add_worksheet('');
    $rowno = 0;
    foreach ($export as $row) {
        $colno = 0;
        foreach($row as $col) {
            $worksheet[0]->write($rowno, $colno, $col);
            $colno++;
        }
        $rowno++;
    }
    $workbook->close();
    die;
}

?>